package com.kgc.onlineLife.controller;

import com.kgc.onlineLife.bean.Category;
import com.kgc.onlineLife.service.CategoryService;
import com.kgc.onlineLife.service.PageService;
import com.kgc.onlineLife.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

/**
 * Created On : 2022/4/19.
 * <p>
 * Author     : nainai
 * <p>
 * Description: 商品分类Controller
 */
@Controller
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private PageService pageService;

    /**
     * @author : nainai
     * @date   : 2022/4/16
     * @param  : [java.util.Map<java.lang.String,java.lang.Object>, java.lang.Integer]
     * @return : java.lang.String
     * @description : 商品分类管理页面请求
     */
    @RequestMapping("/categories/{pageNo}")
    public String Categories(Map<String, Object> map, @PathVariable("pageNo") Integer pageNo){
        // 调用业务接口,查询分页
        Page page = pageService.queryPage("category");
        page.setPageNo(pageNo);
        map.put("page", page);

        // 标识是否为搜索的内容分页
        map.put("searchPage", false);

        // 调用业务接口，查询商品分类，带分页
        map.put("categories", categoryService.queryCategories(page));

        return "/Admin/category_list";
    }

    /**
     * @author : nainai
     * @date   : 2022/4/17
     * @param  : [java.util.Map<java.lang.String,java.lang.Object>, java.lang.String]
     * @return : java.lang.String
     * @description : 分类搜索
     */
    @RequestMapping(value = "/searchCategory", method = RequestMethod.GET, params = "name")
    public String searchCategory(Map<String, Object> map, String name){
        // 为空时查全部
        if (name == null || name == "") {
            return "redirect:/categories/1";
        }
        // 调用业务接口，查询分页
        Page page = pageService.queryPageByName(name, "category");
        map.put("page", page);

        // 搜索时不显示分页
        map.put("searchPageFlag", true);

        map.put("categories", categoryService.queryCategoriesByName(name));

        return "/Admin/category_list";
    }

    @RequestMapping(value = "/deleteCategory/{id}", method = RequestMethod.DELETE)
    public String deleteCategory(@PathVariable("id") Integer id){
        // 调用业务接口,删除分类
        if (categoryService.removeCategory(id)){
            System.out.println("删除成功！");
        } else {
            System.out.println("删除失败！");
        }

        return "redirect:/categories/1";
    }

    /**
     * @author : nainai
     * @date   : 2022/4/17
     * @param  : []
     * @return : java.lang.String
     * @description : 新增分类
     */
    @RequestMapping(value = "/addCategory", method = RequestMethod.POST)
    public String addCategory(String cname, Map<String, Object> map){
        // 调用业务接口,新增分类
        if (categoryService.addCategory(cname)){
            System.out.println("新增成功！");
        } else {
            // 前端标识是否存在相同分类名称
            map.put("insertMsg", true);
            map.put("cname", cname);
            System.out.println("新增失败！");
            return "/Admin/category_add";
        }

        return "redirect:/categories/1";
    }

    /**
     * @author : nainai
     * @date   : 2022/4/17
     * @param  : [java.lang.Integer]
     * @return : java.lang.String
     * @description : 跳转到修改商品分类页面
     */
    @RequestMapping(value = "/toModCategory/{cid}")
    public String toModCategory(@PathVariable("cid") Integer cid, Map<String, Object> map){
        // 调用业务接口，获取该分类信息
        map.put("category", categoryService.queryCategoryById(cid));

        return "/Admin/category_edit";
    }

    @RequestMapping(value = "/modCategory/{cid}")
    public String modCategory(@PathVariable("cid") Integer cid, String cname, Map<String, Object> map){
        // 调用业务接口，修改分类信息
        if(categoryService.modCategoryById(cid, cname)){
            System.out.println("修改成功！");
        } else {
            // 前端标识是否存在相同分类名称
            Category category = new Category();
            category.setName(cname);
            category.setId(cid);
            map.put("insertMsg", true);
            map.put("category",category);
            System.out.println("修改失败！");
            return "/Admin/category_edit";
        }

        return "redirect:/categories/1";
    }
}
